<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" layout="inline">
      <a-form-model-item :label="`${operateType === 'refuse' ?'拒绝':'退库'}原因`" prop="reason">
        <a-input class="w-full" :maxLength="200" v-model="form.reason" placeholder="请输入"/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>


<script lang="ts">
import {Component, Vue, Ref, Prop, Emit} from "vue-property-decorator";
import {STable} from '@/components'
import {
  emsReturnPlanOrderAudit,
  emsReturnPlanOrderReissue
} from "@/api/modular/main/emsordermaterial/emsOrderMaterialManage";
import {debounce} from "xijs";

@Component({
  components: {
    STable,
  }
})
export default class EmsMaterialRelaunchOrRefuseModal extends Vue {
  @Prop() operateType!: 'refuse' | 'relaunch'
  
  @Emit()
  private handleOk() {
  
  }
  
  @Ref() readonly table!: any
  @Ref() readonly ruleForm!: any
  
  visible = false
  title = ''
  confirmLoading = false
  form = {
    orderId: '',
    reason: '',
  }
  rules = {
    reason: [
      {required: true, message: '请输入原因', trigger: 'blur'},
    ],
  }
  emsOrder = {}
  materialType = this.$businessEnum.MATERIAL_TYPE_ENUM.packing.id
  
  
  show(record) {
    console.log(this.operateType)
    if (this.operateType === 'refuse') {
      this.title = '拒绝'
    }
    
    if (this.operateType === 'relaunch') {
      this.title = '重新发起'
    }
    this.visible = true
    this.emsOrder = record
    this.form.orderId = record.orderId
  }
  
  handleSubmit = debounce(this.submit, 1000)
  
  
  submit() {
    console.log(this.$refs)
    console.log(this.ruleForm)
    this.ruleForm?.validate(valid => {
      if (valid) {
        if (this.operateType === 'refuse') {
          emsReturnPlanOrderAudit({
            ...this.form,
            // 1待审核 2通过 3拒绝 4取消 5已入库
            executeStatus: 3,
          }).then(res => {
            //@ts-ignore
            if (res.success) {
              this.$message.info('拒绝成功')
              this.handleCancel()
              this.handleOk()
            } else {
              //@ts-ignore
              this.$message.error('拒绝失败：' + res.message)
            }
          })
        }
        
        if (this.operateType === 'relaunch') {
          emsReturnPlanOrderReissue(this.form).then(res => {
            //@ts-ignore
            if (res.success) {
              this.$message.info('重新发起成功')
              this.handleCancel()
              this.handleOk()
              
            } else {
              //@ts-ignore
              this.$message.error('重新发起失败：' + res.message)
            }
          })
        }
      } else {
        console.log('error submit!!');
        return false;
      }
    });
    
    
  }
  
  handleCancel() {
    this.visible = false
    this.ruleForm?.resetFields()
  }
  
  
}
</script>


<style scoped lang="less">
/deep/ .ant-form-item {
  display: flex !important;
  
  .ant-form-item-control-wrapper {
    flex: 1;
  }
}

</style>